package com.hundsun.s3.platform.backstage.action.login;

import com.hundsun.jresplus.common.util.StringUtil;
import com.hundsun.s3.common.util.BizSecurity;
import com.hundsun.s3.common.util.PropsUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * com.hundsun.s3.platform.backstage.action
 *
 * @Description:登录Action
 * @Author: zhanglei23613
 * @Date: Created in 10:19 2018/2/7 0007
 * V1.0 10:19 2018/2/7 0007
 */

@Controller
public class LoginAction {

    @RequestMapping(value="/login.htm")
    public void login(ModelMap model){
    /**
     * @Description:
     * @param:
     * @return: void
     * @Author: zhanglei23613
     * @Date: 10:21 2018/2/7 0007
     * V1.0
     */
//        model.put("userName", "admin");
//        model.put("password", "123456");
        model.put("key1", BizSecurity.key1);
        model.put("key2", BizSecurity.key2);
        model.put("key3", BizSecurity.key3);
    }

    @RequestMapping(value="/submitLogin.htm")
    public String submitLogin(String user_name, String password, ModelMap model, HttpSession session){
        /**
         * @Description:登录
         * @param: []
         * @return: java.lang.String
         * @Author: zhanglei23613
         * @Date: 10:24 2018/2/7 0007
         * V1.0
         */
        String errorInfo = "";
        boolean flag = false;
        if(StringUtil.isBlank(user_name)){
            errorInfo = "用户名不能为空";
            flag = false;
        }else if(StringUtil.isBlank(password)){
            errorInfo = "登录密码不能为空";
            flag = false;
        }else{
            Properties props= PropsUtils.loadProps("account.properties");
            String correctUserName=PropsUtils.getString(props,"userName");
            String correctPassword=PropsUtils.getString(props,"password");
            String desPwd = BizSecurity.desDecrypt(password, BizSecurity.key1, BizSecurity.key2, BizSecurity.key3);
            if(!(correctUserName.equals(user_name)&&correctPassword.equals(desPwd))){
                flag=false;
                errorInfo="用户名或密码不正确,请重新输入";
            }else {
                flag = true;
                String nickName = "系统管理员";
                session.setAttribute("nick", "系统管理员");
            }
        }

        if(!flag){
            model.put("errorInfo", errorInfo);
            return "forward:/login.htm";
        }else{
            return "redirect:/index.htm";
        }
    }

    @RequestMapping(value="/logout.json")
    public @ResponseBody
    Map<String,Object> logout(HttpSession session, ModelMap model){
        Map<String,Object> map=new HashMap<String,Object>();
        try {
            session.removeAttribute("nick");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg","注销失败");
            return map;
        }
        map.put("msg","注销成功");
        return map;

    }
}
